package com.sxbbc.plat.controller.jz;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sxbbc.common.core.service.ISysMenusService;
import com.sxbbc.plat.util.SystemLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.function.common.RestResponse;
import com.sxbbc.common.basics.utils.validcode.VerifyCode;
import com.sxbbc.common.core.entity.jz.SysMenus;
import com.sxbbc.common.core.entity.jz.SysUser;
import com.sxbbc.plat.common.MappingUtils;
import com.sxbbc.plat.service.PlatLoginService;
import com.sxbbc.plat.service.StatisService;
import com.sxbbc.plat.util.PlatSession;

/**
 * @author leehao
 * @description 登录页/登录/欢迎页/首页
 * @date 2019-04-26 09:31
 */
@Controller
public class PlatController {
    @Autowired
    private PlatLoginService loginService;
    @Autowired
    private ISysMenusService sysMenusService;
    @Autowired
    private StatisService statisSercvice;

    /**
     * 页面 - 登录页面
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = MappingUtils.SHOW_LOGIN)
    public ModelAndView showLogin() {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_LOGIN);
        return view;
    }

    /**
     * 验证码
     * @param request
     * @param response
     * @author leehao
     * @return void
     */
    @RequestMapping(value = MappingUtils.VERIFY_IMAGE)
    public void verifyImg(HttpServletRequest request, HttpServletResponse response) {
        VerifyCode verifyCode = VerifyCode.getInstance();
        verifyCode.createValidimg(response);
        PlatSession.setValidCode(request, verifyCode.getText());
    }

    /**
     * 去登录操作
     *
     * @param request   session
     * @param login     登录名
     * @param password  密码
     * @param validCode 验证码
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = MappingUtils.DO_LOGIN)
    @ResponseBody
    @SystemLog(methods = "用户登录", module = "用户管理")
    public RestResponse doLogin(HttpServletRequest request, String login,
                                String password, String validCode) {
        return loginService.doLogin(request, login, password, validCode);
    }

    /**
     * 首页
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = MappingUtils.SHOW_INDEX)
    public ModelAndView showHome(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_INDEX);
        /*QueryWrapper<SysMenus> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");*/
        //List<SysMenus> menu = sysMenusService.list(wrapper);
        List<SysMenus> menu = PlatSession.getMenu(request);
        SysUser sysUser = PlatSession.getSysUser(request);
        //InfoDomain domain =Configue.infoDomain;
        //InfoWebsite Info = infoWebsiteMapper.getOne();
        view.addObject("list", menu);
        view.addObject("user", sysUser);
        //view.addObject("pcurl", domain.getPcurl());
        //view.addObject("Info", Info);
        return view;
    }

    /**
     * 欢迎页面
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = MappingUtils.SHOW_WELCOME)
    public ModelAndView showWelcome() {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_WELCOME);
        Map<String, Object> map = statisSercvice.queryWelcomeData();
        view.addObject("map", map);
        return view;
    }

    /**
     * 退出登录
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = MappingUtils.EXIT)
    public ModelAndView exit(HttpServletRequest request){
        PlatSession.exit(request);
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_LOGIN);
        return view;
    }
}
